<?php
namespace app\app\controller;
use think\Db;
class Cart extends Common
{
    //获取购物车列表
    public function get_list()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $msg['state'] = 1;
//        $page = request()->request('page')?request()->request('page'):1;
//        $pagesize = request()->request('pagesize')?request()->request('pagesize'):10;
//        $data = Db::table('address')->field('lng,lat,name,phone,address,address_detail')->where(['uid'=>$uid,'def'=>1])->find();
        $data['goods'] = Db::table('cart')->field('goods_id,number')->where('uid',$uid)->select();
        $price = 0;
        $goods_number = 0;
        foreach ($data['goods'] as $k => $v){
            $goods = Db::table('service')->where('id',$v['goods_id'])->find();
            $data['goods'][$k]['price'] = strval($goods['price']);
            $data['goods'][$k]['image1'] = strval($goods['image1']);
            $data['goods'][$k]['name'] = strval($goods['title']);
            $goods_number += $v['number'];
            $price += $v['number']*$goods['price'];
        }
        $data['goods_number'] = $goods_number;
        $data['price'] = strval($price);
        $msg['data'] = $data;
        $msg['msg'] = '获取成功';
        return json($msg);
    }

    //编辑购物车
//    public function edit()
//    {
//        $token = request()->request('token');
//        $uid = token_decode($token);
//        $goods_id = request()->request('goods_id');
//        $number = request()->request('number');
//        $msg['state'] = 0;
//        $msg['data'] = $number;
//        if (empty($uid) || empty($goods_id) || $number == '') {
//            $msg['msg'] = '参数错误';
//        } else {
//            $check = Db::table('cart')->where(['uid' => $uid,'goods_id'=>$goods_id])->count();
//            if ($check > 0) {
//                $msg['state'] = 1;
//                if ($number <= 0) {
//                    $msg['msg'] = '删除成功';
//                    Db::table('cart')->where(['uid' => $uid,'goods_id'=>$goods_id])->delete();
//                } else {
//                    $msg['msg'] = '修改数量成功';
//                    Db::table('cart')->where(['uid' => $uid,'goods_id'=>$goods_id])->update(['number' => $number]);
//                }
//            } else {
//                if($number > 0){
//                    $msg['state'] = 1;
//                    $msg['msg'] = '加入购物车成功';
//                    $data['uid'] = $uid;
//                    $data['goods_id'] = $goods_id;
//                    $data['number'] = $number;
//                    Db::table('cart')->insert($data);
//                }else{
//                    $msg['msg'] = '商品已从购物车移除';
//                }
//            }
//        }
//        return json($msg);
//    }

    //扩大范围查询//变动下双向查询用户查询司机,司机查询订单
    function big($lng,$lat,$scope,$type){
        $pi = $this->get_lat_lng($scope);
        $this_time = time();
//        $count_sql = 'select count(id) from user where business=1 and isExpress=1 and active_time>'.($this_time-300).' and lat > '.$lat.'-'.$pi.' and lat < '.$lat.'+'.$pi.' and lng > '.$lng.'-'.$pi.' and lng < '.$lng.'+'.$pi.' order by ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(('.$lng.'* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 asc';
        $type_sql = 'select express_type from express_type_order where order_type like \'%,'.$type.',%\'';
        $count_sql = 'select count(id) from user where status=1 and business=1 and active_time>'.($this_time-300).' and isExpress=1 and express_type in('.$type_sql.') and lat > '.$lat.'-'.$pi.' and lat < '.$lat.'+'.$pi.' and lng > '.$lng.'-'.$pi.' and lng < '.$lng.'+'.$pi.' order by ACOS(SIN(('.$lat.' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS(('.$lat.' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS(('.$lng.'* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 asc';
        $express = Db::table('user')->query($count_sql);
        return $express[0]['count(id)'];
    }

    public function count_charge()
    {
        $token = request()->request('token');
        $uid = token_decode($token);
        $type = 'fast';
        $lng = request()->request('lng');
        $lat = request()->request('lat');
        $area = request()->request('area')?request()->request('area'):'郓城县';
        $msg['state'] = 0;
        $msg['data'] = [];
        if(empty($lng) || empty($lat)){
            $msg['data'] = json('');
            $msg['msg'] = '参数错误';
        }else{
            $cart_array = Db::table('cart')->where('uid',$uid)->select();
            if(count($cart_array) == 0){
                $msg['data'] = json('');
                $msg['msg'] = '没有选购商品';
            }else{
                //跑腿费
                $min_size = 0;
                $this_time = time();
                $aid = Db::table('area')->where('name','like','%'.$area.'%')->value('id');
                $express_config = Db::table('express_config')->where('aid',$aid)->find();
                $scope = $express_config['scope'];
                $big_scope = $scope*2;
                $result_scope = $min_size;
                while ($scope <= $big_scope && $result_scope == $min_size){
                    $result_scope = $this->big($lng,$lat,$scope,$type);
                    $scope++;
                }
                if($result_scope <= $min_size){
                    $msg['data'] = json('');
                    $msg['msg'] = '附近暂无配送员';
                }elseif($result_scope > $min_size) {
                    $msg['state'] = 1;
                    //商品费用
                    $shop_price = 0;
                    foreach ($cart_array as $k => $v){
                        $goods = Db::table('service')->where('id',$v['goods_id'])->find();
                        $shop_price += $v['number']*$goods['price'];
                    }
                    //距离费用
                    $pi = $this->get_lat_lng($express_config['scope']);
                    $type_sql = 'select express_type from express_type_order where order_type like \'%,'.$type.',%\'';
                    $sql = 'select id as express_id,lng,lat,name from user where status=1 and business=1 and active_time>' . ($this_time - 300) . ' and isExpress=1 and express_type in(' . $type_sql . ') and lat > ' . $lat . '-' . $pi . ' and lat < ' . $lat . '+' . $pi . ' and lng > ' . $lng . '-' . $pi . ' and lng < ' . $lng . '+' . $pi . ' order by ACOS(SIN((' . $lat . ' * 3.1415) / 180 ) *SIN((lat * 3.1415) / 180 ) +COS((' . $lat . ' * 3.1415) / 180 ) * COS((lat * 3.1415) / 180 ) *COS((' . $lng . '* 3.1415) / 180 - (lng * 3.1415) / 180 ) ) * 6380 asc';
                    $express = Db::table('user')->query($sql);
                    $scope = round($this->getDistance($express[0]['lat'], $express[0]['lng'], $lat, $lng) / 1000, 2);
                    $this_time = date('H');
                    if ($this_time > 8 && $this_time <= 20) {
                        $express_charge = $express_config['white_charge'];
                    } else {
                        $express_charge = $express_config['night_charge'];
                    }
                    $pt_price = ($express_charge * $scope) + $express_config['fast_charge'];
                    $msg['data']['shop_price'] = $shop_price;
                    $msg['data']['express_charge'] = $pt_price;
                    $msg['msg'] = '获取成功';
                }
            }
        }
        return json($msg);
    }

}